package com.yict.common.util;

import java.math.BigDecimal;

import org.apache.commons.lang.math.NumberUtils;

import com.yict.common.Constants;

public class NumberUtil extends NumberUtils {
	/**
	 * 判断是否相等
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean equals(Number a, Number b) {
		if (a == null && b == null)
			return true;

		if (a != null && b != null)
			return a.doubleValue() == b.doubleValue();

		return false;
	}

	/**
	 * 根据规则比较
	 * 
	 * @param a
	 * @param b
	 * @param rule
	 * @return
	 */
	public static boolean compare(Number a, Number b, String rule) {

		if (rule.equals(Constants.RULE_BIG_EQUAL)) {
			if (a == null || b == null)
				return false;

			return a.doubleValue() >= b.doubleValue();
		} else if (rule.equals(Constants.RULE_BIG)) {
			if (a == null || b == null)
				return false;

			return a.doubleValue() > b.doubleValue();
		} else if (rule.equals(Constants.RULE_EQUAL))
			return equals(a, b);

		return false;
	}

	/**
	 * BigDecimal转Long
	 * 
	 * @param decimal
	 * @return
	 */
	public static Long BigDecimal2Long(BigDecimal decimal) {
		Long result = null;

		if (decimal != null) {
			result = new Long(decimal.longValue());
		}

		return result;
	}

	/**
	 * BigDecimal转Long
	 * 
	 * @param decimal
	 * @return
	 */
	public static Double BigDecimal2Double(BigDecimal decimal) {
		Double result = null;

		if (decimal != null) {
			result = new Double(decimal.doubleValue());
		}

		return result;
	}

	public static int toInt(Object number) {
		if (number == null)
			return 0;

		if (number instanceof Number)
			return ((Number) number).intValue();

		return 0;
	}

	public static Boolean toBoolean(Object number) {
		if (number == null)
			return null;

		if (number instanceof Boolean)
			return (Boolean) number;

		return new Boolean(toInt(number) == Constants.YES.intValue());
	}

	public static boolean isNull(Number number) {
		if (number == null)
			return true;
		return false;
	}

	public static boolean isNotNull(Number number) {
		return !isNull(number);
	}

	public static void main(String args[]) {
		Long l = null;
		System.out.println(isNull(l));
	}
	
	public static float toFloat(Object number) {
		if (number == null)
			return 0;

		if (number instanceof Number)
			return ((Number) number).floatValue();

		return 0;
	}
}
